﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KenLib.BusinessManager;
using KenLib.BusinessObject;
using DataInfo;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Web.Services;

public partial class JOINT_frmJ_TraineeResumeBInput : System.Web.UI.Page
{
    事業内容Manager _事業内容Manager = new 事業内容Manager();
    応募情報Manager _応募情報Manager = new 応募情報Manager();

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Page load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_TraineeResumeBInput");
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("来社時　情報確認・修正");

        txt_名.Focus();
        RFV_名.ErrorMessage = GetError("ER00001");
        RFV_姓.ErrorMessage = GetError("ER00001");
        RFV_身長.ErrorMessage = GetError("ER00001");
        RFV_体重.ErrorMessage = GetError("ER00001");
        RFV_ddl_視力.ErrorMessage = GetError("ER00007");
        RFV_配偶者有無.ErrorMessage = GetError("ER00007");
        RFV_家族構成.ErrorMessage = GetError("ER00001");
        RFV_海外渡航歴有無.ErrorMessage = GetError("ER00007");
        RFV_在日親族有無.ErrorMessage = GetError("ER00007");
        REV_Email.ErrorMessage = GetError("ER00002");
        CV_電話番号1.ErrorMessage = GetError("ER00001");
        CV_電話番号2.ErrorMessage = GetError("ER00001");
        CV_電話番号3.ErrorMessage = GetError("ER00001");
        CV_携帯番号1.ErrorMessage = GetError("ER00001");
        CV_携帯番号2.ErrorMessage = GetError("ER00001");
        CV_携帯番号3.ErrorMessage = GetError("ER00001");
        CV_身長.ErrorMessage = GetError("ER00016");
        CV_体重.ErrorMessage = GetError("ER00017");
        RFV_就業経験有無.ErrorMessage = GetError("ER00007");
        RFV_経験作業名1.ErrorMessage = GetError("ER00007");
        RFV_具体的仕事内容.ErrorMessage = GetError("ER00001");
        CV_経験作業名1.ErrorMessage = GetError("ER00007");
        CV_経験作業名21.ErrorMessage = GetError("ER00009");
        CV_経験作業名3.ErrorMessage = GetError("ER00009");
        RFV_写真.ErrorMessage = GetError("ER00002");
        CV_Email.ErrorMessage = GetError("ER00009");
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.Params["id"]))
            {
                int dem = 0;
                Int32.TryParse(Request.Params["id"], out dem);
                if (dem != 0)
                {
                    Show_ddl_経験作業名1();
                    Show_ddl_経験作業名2();
                    Show_ddl_経験作業名3();
                    ShowLoad(dem);
                }
                else
                {
                    Response.Redirect("frmJ_TraineeInterviewList.aspx");
                }
            }
            else
            {
                Response.Redirect("frmJ_TraineeInterviewList.aspx");
            }
        }
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Go to frmJ_TraineeInterviewList
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnキャンセル_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_TraineeInterviewList.aspx");
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Update database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn受付完了_Click(object sender, EventArgs e)
    {

        if ((fu_写真.PostedFile.ContentLength / 1024) > 500)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + GetError("ER00019") + "');</script>", false);
            return;
        }
        else
        {
            if (CV_Email.IsValid == true)
            {
                応募情報 _応募情報 = Get応募情報();
                if (_応募情報Manager.frmJ_TraineeResumeBInput_Update(_応募情報) == 1)
                {
                    Response.Redirect("frmJ_TraineeInterviewList.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('データベースに接続ができません');</script>");
                    return;
                }
            }
           
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get object 応募情報
    /// </summary>
    /// <returns></returns>
    public 応募情報 Get応募情報()
    {
        応募情報 _応募情報 = new 応募情報();

        if (!string.IsNullOrEmpty(Request.Params["id"]))
        {
            int dem = 0;
            Int32.TryParse(Request.Params["id"], out dem);
            if (dem != 0)
            {
                _応募情報.ID = dem;
                _応募情報.姓 = ConvertHTMLtoText(txt_名.Text);
                _応募情報.名 = ConvertHTMLtoText(txt_姓.Text);
                if (fu_写真.PostedFile.FileName != "" && fu_写真.PostedFile.FileName != null)
                {
                    Bitmap bmp = new Bitmap(fu_写真.FileContent);
                    string str_FileName = saveImage(bmp);
                    _応募情報.写真 = str_FileName;
                    string path = Server.MapPath("../Images/Joint_Staff/") + str_FileName;
                    string source = Session["frmJ_TraineeResumeBInput_Cache"].ToString();
                    File.Copy(source, path);
                    if (Session["frmJ_TraineeResumeBInput_写真"] != null && Session["frmJ_TraineeResumeBInput_写真"].ToString() != "")
                    {
                        string OldPath = Server.MapPath("../Images/Joint_Staff/") + Session["frmJ_TraineeResumeBInput_写真"].ToString();
                        File.Delete(OldPath);
                    }
                }
                else
                {
                    DataTable dt = frmJ_TraineeResumeBInput_SelectByID(dem);
                    _応募情報.写真 = dt.Rows[0]["写真"].ToString();
                }
                _応募情報.身長 = ConvertHTMLtoText(txt_身長.Text);
                _応募情報.体重 = ConvertHTMLtoText(txt_体重.Text);

                _応募情報.視力 = Convert.ToInt32(ddl_視力.SelectedValue);
                
                if (ddl_配偶者有無.SelectedValue == "-1")
                {
                    _応募情報.配偶者有無 = null;
                }
                else if (ddl_配偶者有無.SelectedValue == "0")
                {
                    _応募情報.配偶者有無 = false;
                }
                else
                {
                    _応募情報.配偶者有無 = true;
                }

                _応募情報.家族構成 = ConvertHTMLtoText(txt_家族構成.Text);

                if (ddl_海外渡航歴有無.SelectedValue == "-1")
                {
                    _応募情報.海外渡航歴有無 = null;
                }
                else if (ddl_海外渡航歴有無.SelectedValue == "0")
                {
                    _応募情報.海外渡航歴有無 = false;
                }
                else
                {
                    _応募情報.海外渡航歴有無 = true;
                }

                if (ddl_在日親族有無.SelectedValue == "-1")
                {
                    _応募情報.在日親族有無 = null;
                }
                else if (ddl_在日親族有無.SelectedValue == "0")
                {
                    _応募情報.在日親族有無 = false;
                }
                else
                {
                    _応募情報.在日親族有無 = true;
                }

                _応募情報.在日親族状況 = ConvertHTMLtoText(txt_在日親族状況.Text);
                string str_電話番号 = ConvertHTMLtoText(txt_電話番号1.Text) + "-" + ConvertHTMLtoText(txt_電話番号2.Text) + "-" + ConvertHTMLtoText(txt_電話番号3.Text);
                if (str_電話番号 == "--")
                {
                    _応募情報.電話番号 = "";
                }
                else
                {
                    _応募情報.電話番号 = str_電話番号;
                }
                string str_携帯番号 = ConvertHTMLtoText(txt_携帯番号1.Text) + "-" + ConvertHTMLtoText(txt_携帯番号2.Text) + "-" + ConvertHTMLtoText(txt_携帯番号3.Text);
                if (str_携帯番号 == "--")
                {
                    _応募情報.携帯番号 = "";
                }
                else
                {
                    _応募情報.携帯番号 = str_携帯番号;
                }

                _応募情報.Email = ConvertHTMLtoText(txt_Email.Text);
                _応募情報.現住所 = ConvertHTMLtoText(txt_現住所.Text);
                _応募情報.趣味 = ConvertHTMLtoText(txt_趣味.Text);

                if (ddl_日本語学習経験.SelectedValue != "-1")
                {
                    _応募情報.日本語学習経験 = ddl_日本語学習経験.SelectedValue.ToString();
                }

                if (ddl_就業経験有無.SelectedValue == "0")
                {
                    _応募情報.就業経験有無 = false;
                }
                else
                {
                    _応募情報.就業経験有無 = true;
                    if (ddl_経験作業名1.SelectedValue == "-1")
                    {
                        _応募情報.経験作業名1 = null;
                    }
                    else
                    {
                        _応募情報.経験作業名1 = Convert.ToInt32(ddl_経験作業名1.SelectedValue.ToString());
                    }

                    if (ddl_経験作業名2.SelectedValue == "-1")
                    {
                        _応募情報.経験作業名2 = null;
                    }
                    else
                    {
                        _応募情報.経験作業名2 = Convert.ToInt32(ddl_経験作業名2.SelectedValue.ToString());
                    }

                    if (ddl_経験作業名3.SelectedValue == "-1")
                    {
                        _応募情報.経験作業名3 = null;
                    }
                    else
                    {
                        _応募情報.経験作業名3 = Convert.ToInt32(ddl_経験作業名3.SelectedValue.ToString());
                    }


                    _応募情報.具体的仕事内容 = ConvertHTMLtoText(txt_具体的仕事内容.Text);
                }



            }
        }



        return _応募情報;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in dropdowlist 経験作業名1
    /// </summary>
    public void Show_ddl_経験作業名1()
    {
        DataTable dt = new DataTable();
        dt = _事業内容Manager.frmJ_TraineeResumeBInput_Select();
        ddl_経験作業名1.DataSource = dt;
        ddl_経験作業名1.DataTextField = "事業内容";
        ddl_経験作業名1.DataValueField = "ID";
        ddl_経験作業名1.DataBind();
        ListItem item = new ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_経験作業名1.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in dropdowlist 経験作業名2
    /// </summary>
    public void Show_ddl_経験作業名2()
    {
        DataTable dt = new DataTable();
        dt = _事業内容Manager.frmJ_TraineeResumeBInput_Select();
        ddl_経験作業名2.DataSource = dt;
        ddl_経験作業名2.DataTextField = "事業内容";
        ddl_経験作業名2.DataValueField = "ID";
        ddl_経験作業名2.DataBind();
        ListItem item = new ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_経験作業名2.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in dropdowlist 経験作業名3
    /// </summary>
    public void Show_ddl_経験作業名3()
    {
        DataTable dt = new DataTable();
        dt = _事業内容Manager.frmJ_TraineeResumeBInput_Select();
        ddl_経験作業名3.DataSource = dt;
        ddl_経験作業名3.DataTextField = "事業内容";
        ddl_経験作業名3.DataValueField = "ID";
        ddl_経験作業名3.DataBind();
        ListItem item = new ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_経験作業名3.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get error message from id
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            error = dt.Rows[0][2].ToString();
        }
        return error;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character html to Text
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertHTMLtoText(string temp)
    {
        temp = temp.Replace("<", "&lt;");
        temp = temp.Replace(">", "&gt;");
        temp = temp.Replace(Convert.ToString((char)13), "<br/>");
        return temp;

    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character text to html
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertTexttoHTML(string temp)
    {
        temp = temp.Replace("&lt;", "<");
        temp = temp.Replace("&gt;", ">");
        return temp;
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data in control page
    /// </summary>
    /// <param name="ID"></param>
    public void ShowLoad(int ID)
    {
        DataTable dt = frmJ_TraineeResumeBInput_SelectByID(ID);
        if (dt.Rows.Count > 0)
        {
            txt_姓.Text = ConvertTexttoHTML(dt.Rows[0]["名"].ToString());
            txt_名.Text = ConvertTexttoHTML(dt.Rows[0]["姓"].ToString());
            string str_写真 = dt.Rows[0]["写真"].ToString();
            if (str_写真 != "" && str_写真 != null)
            {
                img_写真.ImageUrl = "../Images/Joint_Staff/" + str_写真;
                Session["frmJ_TraineeResumeBInput_写真"] = str_写真; //Use option if exists img, update new img will delete old img
            }
            else
            {
                img_写真.ImageUrl = "../Images/no-image.jpg";
            }
            txt_身長.Text = dt.Rows[0]["身長"].ToString();
            txt_体重.Text = dt.Rows[0]["体重"].ToString();

            if (dt.Rows[0]["視力"].ToString() == "")
            {
                ddl_視力.SelectedValue = "-1";
            }
            else
            {
                ListItem lst = ddl_視力.Items.FindByValue(dt.Rows[0]["視力"].ToString());
                ddl_視力.SelectedIndex = ddl_視力.Items.IndexOf(lst);
            }


            if (dt.Rows[0]["配偶者有無"].ToString() == "")
            {
                ddl_配偶者有無.SelectedValue = "-1";
            }
            else if (dt.Rows[0]["配偶者有無"].ToString() == "True")
            {
                ListItem lst = ddl_配偶者有無.Items.FindByText("有");
                ddl_配偶者有無.SelectedIndex = ddl_配偶者有無.Items.IndexOf(lst);
            }
            else
            {
                ListItem lst = ddl_配偶者有無.Items.FindByText("無");
                ddl_配偶者有無.SelectedIndex = ddl_配偶者有無.Items.IndexOf(lst);
            }

            txt_家族構成.Text = ConvertTexttoHTML(dt.Rows[0]["家族構成"].ToString());

            if (dt.Rows[0]["海外渡航歴有無"].ToString() == "")
            {
                ddl_海外渡航歴有無.SelectedValue = "-1";
            }
            else if (dt.Rows[0]["海外渡航歴有無"].ToString() == "True")
            {
                ListItem lst = ddl_海外渡航歴有無.Items.FindByText("有");
                ddl_海外渡航歴有無.SelectedIndex = ddl_海外渡航歴有無.Items.IndexOf(lst);
            }
            else
            {
                ListItem lst = ddl_海外渡航歴有無.Items.FindByText("無");
                ddl_海外渡航歴有無.SelectedIndex = ddl_海外渡航歴有無.Items.IndexOf(lst);
            }

            if (dt.Rows[0]["在日親族有無"].ToString() == "")
            {
                ddl_在日親族有無.SelectedValue = "-1";
            }
            else if (dt.Rows[0]["在日親族有無"].ToString() == "True")
            {
                ListItem lst = ddl_在日親族有無.Items.FindByText("有");
                ddl_在日親族有無.SelectedIndex = ddl_在日親族有無.Items.IndexOf(lst);
            }
            else
            {
                ListItem lst = ddl_在日親族有無.Items.FindByText("無");
                ddl_在日親族有無.SelectedIndex = ddl_在日親族有無.Items.IndexOf(lst);
            }

            //在日親族状況
            txt_在日親族状況.Text = ConvertTexttoHTML(dt.Rows[0]["在日親族状況"].ToString());
            //電話番号
            string str_電話番号 = dt.Rows[0]["電話番号"].ToString();
            if (str_電話番号 == "")
            {
                txt_電話番号1.Text = "";
                txt_電話番号2.Text = "";
                txt_電話番号3.Text = "";
            }
            else
            {
                string[] arr_電話番号 = str_電話番号.Split('-');
                txt_電話番号1.Text = arr_電話番号[0].ToString();
                txt_電話番号2.Text = arr_電話番号[1].ToString();
                txt_電話番号3.Text = arr_電話番号[2].ToString();
            }
            //txt_電話番号1.Text = dt.Rows[0]["電話番号"].ToString();


            //携帯番号
            string str_携帯番号 = dt.Rows[0]["携帯番号"].ToString();
            if (str_携帯番号 == "")
            {
                txt_携帯番号1.Text = "";
                txt_携帯番号2.Text = "";
                txt_携帯番号3.Text = "";
            }

            else
            {
                string[] arr_携帯番号 = str_携帯番号.Split('-');
                txt_携帯番号1.Text = arr_携帯番号[0].ToString();
                txt_携帯番号2.Text = arr_携帯番号[1].ToString();
                txt_携帯番号3.Text = arr_携帯番号[2].ToString();
            }
            //Email
            txt_Email.Text = dt.Rows[0]["Email"].ToString();
            //現住所
            txt_現住所.Text = dt.Rows[0]["現住所"].ToString();
            //趣味
            txt_趣味.Text = dt.Rows[0]["趣味"].ToString();

            if (dt.Rows[0]["日本語学習経験"].ToString() == "")
            {
                ddl_日本語学習経験.SelectedValue = "-1";
            }
            else
            {
                ListItem lst = ddl_日本語学習経験.Items.FindByValue(dt.Rows[0]["日本語学習経験"].ToString());
                ddl_日本語学習経験.SelectedIndex = ddl_日本語学習経験.Items.IndexOf(lst);
            }

            //就業経験有無

            if (dt.Rows[0]["就業経験有無"].ToString() == "")
            {
                ddl_就業経験有無.SelectedValue = "-1";
            }
            else if (dt.Rows[0]["就業経験有無"].ToString() == "True")
            {
                ListItem lst = ddl_就業経験有無.Items.FindByText("有");
                ddl_就業経験有無.SelectedIndex = ddl_就業経験有無.Items.IndexOf(lst);
            }
            else
            {
                ListItem lst = ddl_就業経験有無.Items.FindByText("無");
                ddl_就業経験有無.SelectedIndex = ddl_就業経験有無.Items.IndexOf(lst);
            }

            //経験作業名1
            if (dt.Rows[0]["経験作業名1"].ToString() == "" || dt.Rows[0]["経験作業名1"].ToString() == "-1")
            {
                ddl_経験作業名1.SelectedValue = "-1";
            }
            else
            {
                ListItem lst = ddl_経験作業名1.Items.FindByValue(dt.Rows[0]["経験作業名1"].ToString());
                ddl_経験作業名1.SelectedIndex = ddl_経験作業名1.Items.IndexOf(lst);
            }

            //経験作業名2
            if (dt.Rows[0]["経験作業名2"].ToString() == "" || dt.Rows[0]["経験作業名2"].ToString() == "-1")
            {
                ddl_経験作業名2.SelectedValue = "-1";
            }
            else
            {
                ListItem lst = ddl_経験作業名2.Items.FindByValue(dt.Rows[0]["経験作業名2"].ToString());
                ddl_経験作業名2.SelectedIndex = ddl_経験作業名2.Items.IndexOf(lst);
            }

            //経験作業名3
            if (dt.Rows[0]["経験作業名3"].ToString() == "" || dt.Rows[0]["経験作業名3"].ToString() == "-1")
            {
                ddl_経験作業名3.SelectedValue = "-1";
            }
            else
            {
                ListItem lst = ddl_経験作業名3.Items.FindByValue(dt.Rows[0]["経験作業名3"].ToString());
                ddl_経験作業名3.SelectedIndex = ddl_経験作業名3.Items.IndexOf(lst);
            }

            txt_具体的仕事内容.Text = ConvertTexttoHTML(dt.Rows[0]["具体的仕事内容"].ToString());

        }
        else
        {
            Response.Redirect("frmJ_TraineeInterviewList.aspx");
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get 1 record from table 応募情報
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public DataTable frmJ_TraineeResumeBInput_SelectByID(int ID)
    {
        return _応募情報Manager.frmJ_TraineeResumeBInput_SelectByID(ID);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Save img in Cache
    /// </summary>
    /// <param name="bitmap"></param>
    /// <returns></returns>
    public string saveImage(Bitmap bitmap)
    {
        string tmp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        string name = "JSYSTEM-";
        Random random = new Random();
        for (int i = 0; i < 20; i++)
        {
            int index = random.Next(tmp.Length - 1);
            name += tmp[index].ToString();
        }
        string cachePath = Path.Combine(HttpRuntime.CodegenDir, name + ".png");
        //Session.Add("cachePathImahe", cachePath);
        Session["frmJ_TraineeResumeBInput_Cache"] = cachePath;
        bitmap.Save(cachePath, ImageFormat.Png);
        return name + ".png";
    }



    protected void CV_Email_ServerValidate(object source, ServerValidateEventArgs args)
    {
        
        int dem = 0;
        Int32.TryParse(Request.Params["id"], out dem);
        if (dem != 0)
        {
            DataTable dt = new 基本情報Manager().checkEmailExists("応募情報", dem.ToString(), ConvertHTMLtoText(txt_Email.Text),"Email", "ID");
            if (dt.Rows.Count >= 0)
            {
                if (dt.Rows[0]["Email"].ToString() == "0")
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false ;
                }
            }
            else
            {
                args.IsValid = false;
            }
        }
        else
        {
            args.IsValid = false;
        }
       
    }
}
